package clothes.tools.web;

import clothes.tools.pojo.Administrators;
import clothes.tools.pojo.Customer;
import clothes.tools.service.AdministratorsService;
import clothes.tools.service.CustomerService;
import clothes.tools.service.impl.AdministratorsServiceImpl;
import clothes.tools.service.impl.CustomerServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

public class UserServlet extends BaseServlet {
    private CustomerService customerService= new CustomerServiceImp();
    private AdministratorsService administratorsService=new AdministratorsServiceImpl();
    /*
    * 处理登录请求
    * */
    protected void Login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String identity = req.getParameter("identity");
        Customer logincustomer=customerService.login(new Customer(username,password,"null",new BigDecimal(0),"null"));
        Administrators loginadmin = administratorsService.Login(new Administrators(username, password));
//        identity为user是用户，为admin是管理员
        if ("user".toString().equals(identity)){
            if(logincustomer==null){
//                把错误信息，和回显的表单信息，保存到request域中
                req.setAttribute("msg","用户名或密码错误");
                req.setAttribute("username",username);
//                跳回登录页面
                req.getRequestDispatcher("/index.jsp").forward(req,resp);
            }
            else{
//                登录成功
                req.getRequestDispatcher("/client.jsp").forward(req,resp);
            }

        }
        else if("admin".toString().equals(identity)){
            if (loginadmin==null) {
//                把错误信息，和回显的表单信息，保存到request域中
                req.setAttribute("msg","用户名或密码错误");
                req.setAttribute("username",username);
//                跳回登录页面
                req.getRequestDispatcher("/index.jsp").forward(req, resp);
            }
            else{
                req.getRequestDispatcher("/user.jsp").forward(req,resp);
            }

        }
//        如果不选择身份,跳回注册页面
        else{
            req.getRequestDispatcher("/index.jsp").forward(req, resp);
        }


    }
    /*
    * 处理注册请求
    * */
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String password2 = req.getParameter("password2");
        String sex = req.getParameter("sex");
        sex = new String(sex.getBytes("ISO-8859-1"), "UTF-8");
        if(customerService.existsUsername(username)){
            req.setAttribute("msg","用户名已存在");
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            req.getRequestDispatcher("/register.jsp").forward(req,resp);
        }
        else{
            if(password.equals(password2)) {
                customerService.ResistCustomer((new Customer(username, password, sex, new BigDecimal(0), email)));
                req.getRequestDispatcher("/registed_succeed.jsp").forward(req, resp);
            }
            else{
                req.setAttribute("username",username);
                req.setAttribute("msg","两次输入的密码不一致");
                req.setAttribute("email",email);
                req.getRequestDispatcher("/register.jsp").forward(req,resp);
            }
        }
    }
}
